package com.gopay.bis.service.bank.impl.wap;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.BisPayCheckUtils;
import com.gopay.bis.common.util.constants.BisPayConstants;
import com.gopay.bis.manager.bank.Wap.CmbWapB2cManager;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.CmbConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cmb.ebank.pay.CmbB2cMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.WapPayInteractRemoteService;


/**
 * 招商银行 WAP网银 B2C业务实现
 * @CmbWapPayB2cBankServiceImpl.java
 * @author SunXiaoJian
 */

public class CmbWapPayB2CBankServiceImpl implements WapPayInteractRemoteService {
	
	@Resource(name = "cmbWapB2cManager")
	private CmbWapB2cManager cmbWapB2cManager;
	@Override
	public Result pay(BaseMessage base) {
		Result result = new Result(false);
		try {
			CmbB2cMessage messageB2C = new CmbB2cMessage();
			BeanUtils.copyProperties(base, messageB2C);
			CmbB2cMessage signMsg = (CmbB2cMessage) cmbWapB2cManager.sign(messageB2C);
			result.setDefaultModel(signMsg);
			result.setSuccess(true);
    	} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6064);
		}
        return result;
	}
	
	/**
	 * 订单支付后银行返回的内容
	 * @param BaseMessage
	 * @return Result
	 */
	@Override
	public Result bankBack(BaseMessage base) {
		Result result = new Result(false);
		try {
			boolean flag = false;
			CmbB2cMessage messageB2C = new CmbB2cMessage();
			BeanUtils.copyProperties(base, messageB2C);
			flag = (Boolean) cmbWapB2cManager.verify(messageB2C);
			result.setDefaultModel(flag);
			result.setModel("res", messageB2C);
			result.setSuccess(true);
			
			Map<String , String> param = new HashMap<String , String>();		
			// 通知请求后台成功订单
			if(messageB2C.getSucceed().equals("Y")){
				param.put("orderId", messageB2C.getOrderId());
				param.put("amt", messageB2C.getAmt().toString());
				param.put("cardType", "Unknown");
                param.put("notifyNum", "1");
                param.put("payStatus", "00");
                param.put("retCode", "00");
                param.put("retMsg", "验签成功");
                param.put("signData", messageB2C.getSignMessage());
                param.put("isFront", String.valueOf(base.isFront()));
                result.setModel("isNotifyCashier", param);

                // 通知请求后台失败订单
			}else {
				param.put("orderId", messageB2C.getOrderId());
				param.put("amt", messageB2C.getAmt().toString());
				param.put("cardType", "Unknown");
                param.put("notifyNum", "1");
                param.put("payStatus", "01");
                param.put("retCode", "01");
                param.put("retMsg", "验签无效");
                param.put("signData", messageB2C.getSignMessage());
                param.put("isFront", String.valueOf(base.isFront()));
                result.setModel("isNotifyCashier", param);
			}
			
		} catch(GopayException e){
    		BankLogUtils.printServiceImplExceptionLog(e);
    		result.setResultCode(e.getErrCode()); 		
    	} catch (Exception ex) {
			BankLogUtils.printServiceImplExceptionLog(ex);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100E6064);
		}
        return result;
	}


	/**
	 * 向银行发送请求 查询订单详细信息
	 * @param BaseMessage
	 * @return Result
	 */
	@Override
	public Result query(BaseMessage base) {
		// Result 
		Result result = new Result(false);
		try {
			CmbB2cMessage messageB2C = new CmbB2cMessage();
			BeanUtils.copyProperties(base, messageB2C);
			CmbB2cMessage res = (CmbB2cMessage)cmbWapB2cManager.query(messageB2C);
			result.setDefaultModel(res);
			result.setSuccess(true);
			if(BisPayCheckUtils.checkExternalOrderID(messageB2C.getOrderId())){
				Map<String,String> param = new HashMap<String,String>();
				param.put("orderId", messageB2C.getOrderId());
				param.put("amount", messageB2C.getAmount().toString());
				if (res == null) {
					// 查询订单为空
					param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
					param.put("returnMessage", "银行返回对账结果为空");
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
					result.setModel("queryResult", param);
					return result;
				}
				if (!CmbConstants.CMB_QUERY_STATUS_SUCCESS.equals(res.getQueryUrl())) {
		            // 查询订单错误
					param.put("returnCode", BisPayConstants.QUERYSTATE_FAIL);
					param.put("returnMessage", res.getMsg());
					param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);	
					result.setModel("queryResult", param);
					return result;				
				}
				 //成功处理
		        if (StringUtils.equals(CmbConstants.CMB_ORDER_STATUS_SUCCESS,res.getOrderStatus())) {
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);		        	
					param.put("tradeState", BisPayConstants.TRADESTATE_SUCCESS);
					param.put("returnMessage", "交易成功");
		        } else {
					param.put("returnCode", BisPayConstants.QUERYSTATE_SUCCESS);		
		            if(res.getMsg()!=null || !"".equals(res.getMsg())){ 
						param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
						param.put("returnMessage", res.getMsg());
		            }else{
						param.put("tradeState", BisPayConstants.TRADESTATE_OTHER);
						param.put("returnMessage", "招行订单查询未明确");
		            }
		        }
				result.setModel("queryResult", param);
			}
        } catch (GopayException e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(e.getErrCode());
        } catch (Exception e) {
            BankLogUtils.printServiceImplExceptionLog(e);
            result.setResultCode(ProcCodeConstants.PROC_CODE_100E6033);
        }
        return result;
	}

	@Override
	public Result queryBack(BaseMessage base) {
		// 银行的请求反馈
		return null;
	}
	
	
	@Override
	public Result refund(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result dayend(BaseMessage base) {
		// TODO Auto-generated method stub
		return null;
	}

	
}
